package cn.tedu.csmall.product.service.impl;

import cn.tedu.csmall.product.ex.ServiceException;
import cn.tedu.csmall.product.mapper.SkuMapper;
import cn.tedu.csmall.product.pojo.dto.SkuAndNewDTO;
import cn.tedu.csmall.product.pojo.entity.Sku;
import cn.tedu.csmall.product.pojo.vo.SkuStandardVO;
import cn.tedu.csmall.product.service.ISkuService;
import cn.tedu.csmall.product.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Slf4j
@Service
public class SkuServiceImp implements ISkuService, ServiceCode {
    @Autowired
    SkuMapper skuMapper;
    public SkuServiceImp(){
        log.debug("创建业务类:SkuServiceImp");
    }
    @Override
    public void andNew(SkuAndNewDTO skuAndNewDTO) {
     log.debug("开始添加数量,参数:{}",skuAndNewDTO);
        LocalDateTime now=LocalDateTime.now();
        Sku sku=new Sku();
        sku.setGmtCreate(now);
        sku.setGmtModified(now);
        int row=skuMapper.insert(sku);
        if (row!=1){
            String message="添加数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT,message);
        }
    }
    @Override
    public void delete(Long id) {
    log.debug("开始处理删除请求:{}",id);
        SkuStandardVO sku = skuMapper.getSKU(id);
        if (sku==null){
            String message = "删除数据失败！该数据"+id+"不存在!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        int delete = skuMapper.delete(id);
        if (delete!=1){
            String message="删除数据失败！服务器忙，请稍后再一次尝试!";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }
    }
}
